package com.QK.cnstudy.localtask;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class LoadWork {

    String termTitles[] = { "七年级上", "七年级下", "八年级上", "八年级下", "九年级上", "九年级下" };

    public int getTermId(String termTitle) {
        for (int i = 0; i < termTitles.length; i++) {
            if (termTitle.compareTo(termTitles[i]) == 0) {
                return i + 1;
            }
        }
        return 0;
    }

    public List<Work> loadWork(String inPath, int textbookId, String textbookTitle, List<Author> authors)
                    throws Exception {
        List<Work> works = new ArrayList<LoadWork.Work>();
        int termId = 0;
        String termTitle = "";
        InputStream inputStream = new FileInputStream(inPath);
        BufferedReader workDirBufReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));

        String line = "";
        Work work = null;
        int indexBeginFromterm = 0;
        while ((line = workDirBufReader.readLine()) != null) {
            String[] cells = line.split("\t", 6);

            if (cells[0].compareTo("") != 0) {
                termId = getTermId(cells[0]);
                termTitle = cells[0];
                indexBeginFromterm = 0;
            }

            if (indexBeginFromterm >= 3)
                continue;
            indexBeginFromterm++;
            work = new Work();
            work.id = Integer.valueOf(cells[1]);
            work.title = cells[2];
            work.subTitle = cells[3];
            work.authorName = cells[4];
            work.workInfo = cells[5];

            work.termId = termId;
            work.termTitle = termTitle;
            work.textbookId = textbookId;
            work.textbookTitle = textbookTitle;
            Author author = getAuthorByName(authors, work.authorName);
            if (author == null) {
                System.out.println(work.id);
                System.out.println(work.authorName);
            }
            work.authorId = author.id;
            work.authorDetail = author.detail;

            works.add(work);
        }

        workDirBufReader.close();
        inputStream.close();
        return works;
    }

    public Author getAuthorByName(List<Author> authors, String name) {
        for (int i = 0; i < authors.size(); i++) {
            if (authors.get(i).title.compareTo(name) == 0) {
                return authors.get(i);
            }
        }
        return null;
    }

    public List<Author> loadAuthor(String inPath) throws Exception {
        List<Author> authors = new ArrayList<LoadWork.Author>();
        InputStream inputStream = new FileInputStream(inPath);
        BufferedReader BufReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));

        String line = "";
        while ((line = BufReader.readLine()) != null) {
            String cells[] = line.split("\t");
            Author author = new Author();
            author.id = Integer.valueOf(cells[0]);
            author.title = cells[1];
            author.detail = cells[2];
            authors.add(author);
        }
        BufReader.close();
        inputStream.close();
        return authors;
    }

    public void printWorks(List<Work> works, String path) throws Exception {

        FileWriter fileWriter = new FileWriter(path);
        fileWriter.write("[\n");
        for (int i = 0; i < works.size(); i++) {
            if (i != 0) {
                fileWriter.write(",");
            }
            Work work = works.get(i);
            fileWriter.write("\t{\n");
            fileWriter.write("\t\tid:" + work.id + ",title:\"" + work.title + "\",subtitle:\"" + work.subTitle
                            + "\",info:\"" + work.workInfo + "\",\n");
            fileWriter.write("\t\tauthor:{id:" + work.authorId + ",name:\"" + work.authorName + "\",detail:\""
                            + work.authorDetail + "\"},\n");
            fileWriter.write("\t\tterm:{id:" + work.termId + ",title:\"" + work.termTitle + "\"},\n");
            fileWriter.write("\t\ttextbook:{id:" + work.textbookId + ",title:\"" + work.textbookTitle + "\"}\n");
            fileWriter.write("\t}");
        }
        fileWriter.write("]");
        fileWriter.close();

    }

    public static void main(String[] args) throws Exception {

        String dir = "D:\\work\\classic-chinese-study\\资源\\人教版（9篇）\\人教版（9篇）\\";
        LoadWork task = new LoadWork();
        List<Author> authors = task.loadAuthor(dir + "author_ren.txt");
        List<Work> works = task.loadWork(dir + "study_ren.txt", 1, "人教版", authors);

        task.printWorks(works, dir + "work_people.txt");
    }

    public class Work {
        int id;
        String title;
        String subTitle;
        String workInfo;
        int authorId;
        String authorName;
        String authorDetail;
        int termId;
        String termTitle;
        int textbookId;
        String textbookTitle;
        List<Sentence> sentences;
        List<Word> words;
    }

    public class Author {
        int id;
        String title;
        String detail;
    }

    public class Sentence {
        int id;
        String content;
        String translate;
    }

    public class Word {
        int id;
        String content;
        String translate;
    }
}
